const utils = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */

  data:{ 
        isUserLogin: false,
        shopSummary:{
          "ShopID": 0,
          "UserName": "",
          "ShopName": "",
          "HeadImgSrc": "/imgs/icon-userhead-placeholder.png",
          "PostCount": 0
        }
      },
  /**
* 用户点击右上角分享
*/
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      // 来自页面右上角转发按钮
      return {
        title: '快来上车务圈，更多人脉，更多订单！限时免费注册！',
        path: '/pages/index/index',
        imageUrl: '/imgs/share-banner.jpg'
      };
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {     
      var that = this;
      //that.getInitData();  
  },
  getInitData: function () {
    var that = this;
    var isUserLogin = utils.isUserLogin();
    that.setData({
      isUserLogin: isUserLogin,
      shopid: utils.getUserId()
    });
    if (isUserLogin) {
      that.getMyShop();
    }
  },
  getMyShop:function(){
    var that=this;
     utils.getData(
       utils.apiUrl +"/shop/GetShopSummary?shopid="+utils.getUserId(),
       {},
       function(response){
           that.setData({
             shopSummary:response.data
           });
       }
     );
  },
  bindShopEdit:function(){
    var that = this;
    if(!that.data.isUserLogin){
      return;
    }
    wx.navigateTo({
      url: 'shopedit/shopedit?shopid=' + that.data.shopSummary.ShopID
    })
  }, 
  bindShopDetail:function(){    
    var that = this;
    if (!that.data.isUserLogin) {
      return;
    }
    wx.navigateTo({
      url: 'shopdetail/shopdetail?shopid=' + that.data.shopSummary.ShopID
    })
  },
  bindMyPost:function(){
    var that = this;
    if (!that.data.isUserLogin) {
      return;
    }
    wx.navigateTo({
      url: 'mypost/mypost?shopid=' + that.data.shopSummary.ShopID
    })
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getInitData();
    // utils.checkLogin(function () {
    //   that.getMyShop();
    // }, function () {      
    //   wx.redirectTo({
    //     url: '../my/login/login',
    //   })
    // });
  },
  /**
   * 登录
   */
  bindTapLogin: function (e) {
    var that = this;
    wx.showLoading({
      title: '正在登录',
    })
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: utils.apiUrl + '/shop/GetWxLoginInfo',
            data: {
              code: res.code
            },
            success: function (response) {
              var data = response.data;
              if (data.status == 0) {
                //如果已存在此用户，则根据状态跳转到对应的页面
                if (data.data.ShopID > 0) {
                  utils.setUserId(data.data.ShopID);
                  that.getInitData();
                } else {//否则走注册用户流程
                  var openid = data.data.OpenId;
                  var sessionKey = data.data.WxSessionKey;
                  wx.navigateTo({
                    url: '/pages/my/binduser/binduser?openid=' + openid,
                  })
                }
              }
            },
            fail: function () {

            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },
  /**
   * 退出登录
   */
  bindTapLogout:function(e){
    var that=this;
    wx.setStorageSync("userId", "");
    that.getInitData();
  }

  
})